package com.example.newnoke.view.dialog;

import android.content.DialogInterface;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.newnoke.R;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;

import java.io.File;
import java.io.IOException;


/**
 * Created by jiang on 2017/5/10.
 */

public class UPDataDialgo extends BaseBottomDialog {


    String payway = "1";
    PayCallBack payCallBack;

    private TextView mContent;
    private DownloadProgressButton mDownloadProgressButton;
    private String url;
    private ImageView img;

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_up_data;
    }

    @Override
    public void onStart() {
        super.onStart();
        setParams( Gravity.CENTER);
    }

    @Override
    public void bindView(View v) {
       this.getDialog().setCancelable(false);
       this.getDialog().setCanceledOnTouchOutside(false);
        this.getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });


        url = getArguments().getString("url");

        mContent = v.findViewById(R.id.tv_content);
        mDownloadProgressButton = v.findViewById(R.id.download_btn);

        mDownloadProgressButton.setShowBorder(false);
        mDownloadProgressButton.setCurrentText("安装");
        mDownloadProgressButton.postInvalidate();

        mDownloadProgressButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



//                OkGo.getInstance()
//                        .getCommonParams().clear();

                OkGo.<File>get(url)
                        .tag(this)
                        .execute(new FileCallback() {
                            @Override
                            public void onSuccess(Response<File> response) {
                                dismiss();
                                if (payCallBack != null) {
                                    payCallBack.installation(response.body());
                                }
                            }

                            @Override
                            public void onError(Response<File> response) {
                                System.out.println("----------------- " + response.getException().toString());
                                if (payCallBack != null) {
                                    if (response.getException() instanceof IOException){
                                        IOException ioException = (IOException) response.getException();
                                        if ( ioException.getMessage().toString().indexOf("ENOSPC")!=-1) {
                                            payCallBack.onError("磁盘空间不足");
                                        }else {
                                            payCallBack.onError("");
                                        }

                                    }else {
                                        payCallBack.onError("");
                                    }

                                }
                                dismiss();

                            }

                            @Override
                            public void downloadProgress(Progress progress) {

                                mDownloadProgressButton.setProgressText("", progress.fraction * 100);
                            }


                        });
                mDownloadProgressButton.setState(DownloadProgressButton.STATE_DOWNLOADING);
                mDownloadProgressButton.setProgressText("", 0);
                mDownloadProgressButton.setEnabled(false);
            }
        });
        mContent.setText(getArguments().getString("content"));
    }

    void toPay() {
        dismiss();
    }

    @Override
    public float getDimAmount() {
        return 0.4f;
    }

    public void show(FragmentManager fragmentManager, PayCallBack payCallBack) {
        show(fragmentManager, getFragmentTag());
        this.payCallBack = payCallBack;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public interface PayCallBack {
        void installation(File file);

        void onError(String msg);
    }

}
